package de.hska.bestellverwaltung.db;

import static javax.persistence.CascadeType.PERSIST;
import static javax.persistence.CascadeType.REMOVE;
import static javax.persistence.FetchType.EAGER;
import static de.hska.util.EjbConstants.KEINE_ID;
import static de.hska.util.EjbConstants.ERSTE_VERSION;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Id;
import javax.persistence.Version;
import javax.persistence.ManyToOne;
import javax.persistence.JoinColumn;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.hska.kundenverwaltung.db.Kunde;
import de.hska.util.MessagesUtil;

@Entity
@Table(name="bestellung")
public class Bestellung implements java.io.Serializable {
	private static final long serialVersionUID = 7560752199018702446L;
	protected static final Log LOG = LogFactory.getLog(Bestellung.class);
	protected static final boolean DEBUG = LOG.isDebugEnabled();
	protected static final boolean TRACE = LOG.isTraceEnabled();

	@Id
	@GeneratedValue(generator="bestellung_sequence_name")
	@SequenceGenerator(name="bestellung_sequence_name", sequenceName="bestellung_b_id_seq", allocationSize=1)
	@Column(name="b_id", nullable=false)
	private Long id = KEINE_ID;

	@Version
	private int version = ERSTE_VERSION;

	@OneToMany(fetch=EAGER, cascade={PERSIST, REMOVE})
	@JoinTable(name="best_bestpos",
			joinColumns=@JoinColumn(name="bestellung_fk"),
			inverseJoinColumns=@JoinColumn(name="bestellposition_fk"))
	@OrderBy("id ASC")
	private List<Bestellposition> bestellpositionen;

	@ManyToOne(optional=false)
	@JoinColumn(name="kunde_fk")
	private Kunde kunde;

	@ManyToMany(cascade={REMOVE})
	@JoinTable(name="bestellung_lieferung",
			joinColumns=@JoinColumn(name="bestellung_fk"),
			inverseJoinColumns=@JoinColumn(name="lieferung_fk"))
	@OrderBy("lieferNr ASC")
	private List<Lieferung> lieferungen;

	public Bestellung() {
		super();
	}
	
	@PostPersist
	protected void logDbId() {
		if (DEBUG) {
			final String msg = MessagesUtil.getMessage("log.bestellverwaltung.bestellung.create", id, this);
			LOG.debug(msg);
		}
	}

	@PostUpdate
	protected void logNeueVersion() {
		if (TRACE) {
			final String msg = MessagesUtil.getMessage("log.bestellverwaltung.bestellung.update", id, version);
			LOG.trace(msg);
		}
	}

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}

	public int getVersion() {
		return version;
	}
	public void setVersion(int version) {
		this.version = version;
	}

	public List<Bestellposition> getBestellpositionen() {
		return bestellpositionen;
	}
	public void setBestellpositionen(List<Bestellposition> bestellpositionen) {
		this.bestellpositionen = bestellpositionen;
	}

	public Kunde getKunde() {
		return kunde;
	}
	public void setKunde(Kunde kunde) {
		this.kunde = kunde;
	}

	public List<Lieferung> getLieferungen() {
		return lieferungen;
	}
	public void setLieferungen(List<Lieferung> lieferungen) {
		this.lieferungen = lieferungen;
	}

	@Override
	public String toString() {
		return "{id=" + id + ", version=" + version + ", bestellpositionen=" + bestellpositionen + '}';
	}

	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((bestellpositionen == null) ? 0 : bestellpositionen.hashCode());
		result = PRIME * result + version;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Bestellung other = (Bestellung) obj;
		if (bestellpositionen == null) {
			if (other.bestellpositionen != null)
				return false;
		} else if (!bestellpositionen.equals(other.bestellpositionen))
			return false;
		if (version != other.version)
			return false;
		return true;
	}

}